# coding:utf-8
#!/usr/bin/env python


from flask_script import Shell, Manager
from app import app,db

from app.views import *
from app.apis import *
from app.admin import *
import getpass

manager = Manager(app)


# def _make_context():
#     return dict(app=app)

@manager.command
def syncdb_admin():
    """同步ADMIN数据库
    """
    db.create_all(bind=['admin'])
    
@manager.command
def syncdb():
    """同步数据库
    """
    db.create_all()
    
@manager.command
def create_super_user():
    """创建超级用户
    """
    from app.admin.model import AdminUser
    username = raw_input("用户名:")
    while not username:
        username = raw_input("请输入用户名:")
    password = getpass.getpass("密码:")
    while not password:
        password = getpass.getpass("密码:")
    admin_user = AdminUser(username=username,user_type=1)
    admin_user.set_password(password)
    admin_user.add()
    db.session.commit()
    
@manager.command
def test():
    """测试
    """
    print "Hello World"


manager.add_command("shell", Shell(make_context=lambda:dict(app=app)))

if __name__ == '__main__':
    manager.run()
    

